<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace form\element\form;

use form\element\form\traits\Placeholder;

class Tag extends Input
{
    use Placeholder;

    protected $withBackspace = true;

    protected $delimiter = ',';

    protected $placeholderColor = '#666666';

    public function placeholdercolor(string $color)
    {
        $this->placeholder = $color;
        return $this;
    }

    public function delimiter(string $delimiter)
    {
        $this->delimiter = $delimiter;
        return $this;
    }

    public function backspace(bool $backspace = true)
    {
        $this->withBackspace = $backspace;
        return $this;
    }

    public function value($value)
    {
        if (is_array($value)) {
            $this->value = implode($this->delimiter, $value);
        } else {
            $this->value = $value;
        }
        return $this;
    }

    protected function normal(): string
    {
        $label = $this->getLabel();
        $class = $this->getClass();
        $tagsName = $this->name;
        $id = $this->getId();
        $labelClass = $this->getLabelClass();
        $this->js = <<<SCRIPT

$('#$tagsName').tagsInput({
    height:$('#$tagsName').data('height') ? $('#$tagsName').data('height') : '36px',
    width: 'auto',
    defaultText: $('#$tagsName').attr("placeholder"),
    removeWithBackspace: $this->withBackspace,
    delimiter: ['$this->delimiter'],
    placeholderColor:$('#$tagsName').attr('placeholder-color')?$('#$tagsName').attr('placeholder-color'):'#666666'
});

SCRIPT;

        return <<<HTML

<div class="$class">
    <label for="$tagsName" class="$labelClass">$label</label>
    <input class="js-tags-input form-control $tagsName" type="text" id="$id" name="$tagsName" value="$this->value" placeholder="$this->placeholder" placeholder-color="$this->placeholderColor"/>
</div>
HTML;
    }

    public function inline()
    {
        $label = $this->getLabel();
        $class = $this->getClass();
        $tagsName = $this->name;
        $id = $this->getId();
        $labelClass = $this->getLabelClass();

        $this->js = <<<SCRIPT

$('#$tagsName').tagsInput({
    height:$('#$tagsName').data('height') ? $('#$tagsName').data('height') : '36px',
    width: 'auto',
    defaultText: $('#$tagsName').attr("placeholder"),
    removeWithBackspace: $this->withBackspace,
    delimiter: ['$this->delimiter'],
    placeholderColor:$('#$tagsName').attr('placeholder-color')?$('#$tagsName').attr('placeholder-color'):'#666666'
});

SCRIPT;
        return <<<HTML

<div class="$class">
    <label for="$tagsName" class="$labelClass">$label</label>
    <input class="js-tags-input form-control $tagsName" type="text" id="$id" name="$tagsName" value="$this->value" placeholder="$this->placeholder" placeholder-color="$this->placeholderColor"/>
</div>
HTML;
    }
}